package ms.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import ms.jasim.framework.EventSimulator;
import ms.jasim.framework.IJasimAppContext;
import ms.jasim.framework.SolutionSimulator;
import ms.jasim.model.JasimModel;
import ms.jasim.model.editor.views.EventSimulationView;
import ms.jasim.model.editor.views.SolutionSimulationView;
import ms.jasim.model.presentation.JasimEditorPlugin;
import ms.jasim.pddl.PddlSolution;
import ms.utils.JasimUtils;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

public class SimulationUtils {

	static URL baseURL = null;
	
	public static IViewPart showView(String ID) {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IWorkbenchPart activePart = page.getActivePart();
		try {
			IViewPart view = page.showView(ID);
			page.activate(activePart);
			return view;
		} catch (PartInitException e) {
			JasimEditorPlugin.INSTANCE.log(e);
		}
		return null;
	}
	
	public static void simulateSolution(IJasimAppContext context, PddlSolution solution) {
		if (context == null) context = JasimEditorPlugin.INSTANCE.getRootContext();
		JasimModel model = JasimUtils.cloneModel(solution.getProblem().getModel());
		SolutionSimulator sim = new SolutionSimulator(context, model, solution);
		sim.simulate(0, 9999);
		
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		try {
			SolutionSimulationView view = (SolutionSimulationView) page.showView(SolutionSimulationView.ID);
			view.setInput(sim);
		} catch (PartInitException e) {
			JasimEditorPlugin.INSTANCE.log(e);
		}
	}
	
	public static List<SolutionSimulator> simulateSolutions(IJasimAppContext context, Iterable<PddlSolution> solutions) {
		if (context == null) context = JasimEditorPlugin.INSTANCE.getRootContext();
		ArrayList<SolutionSimulator> result = new ArrayList<SolutionSimulator>();
		
		for (PddlSolution sol: solutions) {
			JasimModel model = JasimUtils.cloneModel(sol.getProblem().getModel());
			SolutionSimulator sim = new SolutionSimulator(context, model, sol);
			sim.simulate(0, 9999);
			result.add(sim);
		}
		return result;
	}

	public static void simulateSolutionWithEvent(IJasimAppContext context, PddlSolution solution) {
		if (context == null) context = JasimEditorPlugin.INSTANCE.getRootContext();
		EventSimulator sim = new EventSimulator(context, solution);
		sim.run();
		
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		try {
			EventSimulationView view = (EventSimulationView) page.showView(EventSimulationView.ID);
			view.setSimulation(sim);
		} catch (PartInitException e) {
			JasimEditorPlugin.INSTANCE.log(e);
		}
	}
	
	public static List<EventSimulator> simulateSolutionsWithEvent(IJasimAppContext context, Iterable<PddlSolution> solutions) {
		if (context == null) context = JasimEditorPlugin.INSTANCE.getRootContext();
		ArrayList<EventSimulator> result = new ArrayList<EventSimulator>();
		
		for (PddlSolution solution: solutions) {
			//EList<EventSet> eventSet = solution.getProblem().getModel().getEventModel().getEventSet();
			EventSimulator sim = new EventSimulator(context, solution);
			sim.run();
			result.add(sim);
		}
		
		return result;
	}

	public static URL getResourceURL(String path) throws MalformedURLException {
		if (baseURL == null)
			baseURL = JasimEditorPlugin.INSTANCE.getBaseURL();
		return new URL(baseURL, path);
	}
	
	public static ImageDescriptor getImageDescriptor(String path) {
		try {
			return ImageDescriptor.createFromURL(getResourceURL(path));
		} catch (Exception e) {
			return null;
		}
	}
}
